package top.lyc829.days.controller;


import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lyc829.days.bean.Event;
import top.lyc829.days.bean.User;
import top.lyc829.days.service.EventService;
import top.lyc829.days.service.UserService;
import top.lyc829.days.util.Result;

import java.util.ArrayList;
import java.util.List;

@RestController    //相当于@Controller+@RequestBody
@RequestMapping("/event")
public class EventController {
    @Autowired
    EventService eventService;

    @Autowired
    UserService userService;

    @RequestMapping("/insert")
    public Result insertEvent(@RequestBody JSONObject jsonParam) {

        // 用户名
        String userName = jsonParam.get("userName").toString();
        System.out.println("username: " + userName);

        String eventName = jsonParam.get("eventName").toString();
        long createTime = Long.parseLong(jsonParam.get("createTime").toString());
        String eventDate = jsonParam.get("eventDate").toString();
        String eventNotes = jsonParam.get("eventNotes").toString();

        Result result = new Result(null, false, null);
        try{
            User user = userService.getUserByUserName(userName);
            if (user != null) {
                int userId = user.getId();

                Event eventExist = eventService.findByUserIdAAndCreateTime(userId, createTime);
                if (eventExist == null) {
                    Event event = new Event(userId, eventName, eventDate, createTime, eventNotes);
                    eventService.addEvent(event);
                    result.setMsg("插入成功");
                    result.setSuccess(true);
                } else {

                    if (eventExist.getName().equals(eventName) && eventExist.getDate().equals(eventDate)
                        && eventExist.getNote().equals(eventNotes)) {
                        result.setMsg("已经备份");
                        result.setSuccess(true);
                    } else {
                        //事件被修改，需要更新
                        eventExist.setName(eventName);
                        eventExist.setDate(eventDate);
                        eventExist.setNote(eventNotes);
                        eventService.addEvent(eventExist);
                        result.setMsg("更新成功");
                        result.setSuccess(true);
                    }
                }
            } else {
                result.setMsg("用户不存在");
            }

        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除事件根据用户id和创建时间
     * @param jsonParam json，{”userName“: "", "createTime": 123}
     * @return Result
     */
    @RequestMapping("/delete")
    public Result deleteEvent(@RequestBody JSONObject jsonParam) {
        String userName = jsonParam.get("userName").toString();
        long createTime = Long.parseLong(jsonParam.get("createTime").toString());

        Result result = new Result(null, false, null);
        try{
            User user = userService.getUserByUserName(userName);
            eventService.deleteByUserIdAAndCreateTime(user.getId(), createTime);
            result.setMsg("删除成功");
            result.setSuccess(true);
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 修改某个事件，根据用户id和创建时间
     * @param jsonParam json，{”userName“: "tbz",”eventName“: "hhh", "createTime": 123,
     *                  "eventDate": "2020-01-01, "eventNotes": "note"}
     * @return Result
     */
    @RequestMapping("/update")
    public Result updateEvent(@RequestBody JSONObject jsonParam) {
        String userName = jsonParam.get("userName").toString();
        String eventName = jsonParam.get("eventName").toString();
        long createTime = Long.parseLong(jsonParam.get("createTime").toString());
        String eventDate = jsonParam.get("eventDate").toString();
        String eventNotes = jsonParam.get("eventNotes").toString();

        Result result = new Result(null, false, null);
        try{
            User user = userService.getUserByUserName(userName);
            Event event = eventService.findByUserIdAAndCreateTime(user.getId(), createTime);
            event.setName(eventName);
            event.setDate(eventDate);
            event.setNote(eventNotes);
            eventService.addEvent(event);
            result.setMsg("修改成功");
            result.setSuccess(true);
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 查询所有的事件
     * @param jsonParam
     * @return
     */
    @RequestMapping("/find_all")
    public Result findAllEvent(@RequestBody JSONObject jsonParam) {
        String userName = jsonParam.get("userName").toString();
        String password = jsonParam.get("password").toString();

        Result result = new Result(null, false, null);
        User user = userService.getUserByUserNameAndPassword(userName, password);

        if (user == null) {
            result.setMsg("账号或密码错误");
            return result;
        }

        List<Event> events;


        try{
            events = eventService.getAllEventsByUserId(user.getId());
            if(!events.isEmpty()) {
                result.setMsg("查询成功");
                result.setDetail(events);
                result.setSuccess(true);
            } else {
                result.setMsg("事件为空");
            }
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

}
